home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / vol16n13.zip / OPENTR.ZIP / OT_SRC.ZIP / OLDCODE.CPP < prev    next >
Text File  |  1997-02-18  |  11KB  |  403 lines

  1. // opentrap.cpp - This program interacts with opentrap.vxd, allowing access
  2. //               to any I/O port.
  3. //
  4. //
  5. // Rick Knoblaugh 11/01/96
  6. //
  7. //
  8. //#define STRICT
  9. //#define _MT
  10. #pragma pack(1)
  11. #include <stdio.h>
  12. #include <sys\timeb.h>
  13. #include <time.h>
  14. #include <windows.h>
  15. #include <windowsx.h>
  16. #include <process.h>
  17.  
  18.  
  19.  
  20.  
  21. #define VXD_NAME        "\\\\.\\opentrap.VXD"
  22.  
  23. #define VXD_GET_VER 1
  24. #define VXD_REGISTER_CB 2       //register a callback and logging conditions
  25. #define VXD_REC_DONE    3       //release the buffer VxD reported to us
  26. #define VXD_UPDATE 4            //update logging conditions
  27. #define VXD_GET_VER 1
  28.  
  29. #define DISABLE_LOGGING 1
  30. #define APP_EXITING 16
  31.  
  32.  
  33.  
  34.  
  35. #define IFSFN_OPEN 36           //function codes for file system operations
  36. #define IFSFN_READ 0
  37. #define IFSFN_CLOSE  11         
  38. #define IFSFN_DELETE 31
  39. #define LOG_ALL 0x0ffffffff
  40. #define LOG_ALL_DOS 0x0fffffffe
  41. #define LOG_ONLY_ERRORS 0xfffe
  42. #define REP_BUF_SIZE (60 * 1024)
  43. #define MAX_PATH 260
  44.  
  45.  
  46. LRESULT CALLBACK regvproc(HWND, UINT, WPARAM, LPARAM);
  47. void __stdcall our_callback(DWORD);
  48. void InitUnobsfucator(void);
  49. void format_log_record (struct trap_record *);
  50. void    release_rec(DWORD);
  51. DWORD  __stdcall do_getcalls();
  52.  
  53.  
  54. CRITICAL_SECTION   crit_sec; 
  55. DWORD      thread_id;
  56. HANDLE     stop_flag;
  57. HANDLE     thread_handle;
  58. HANDLE        vxd_handle        = INVALID_HANDLE_VALUE;
  59. HINSTANCE   hinstance;    //Handle of current instance
  60. HWND        handle;
  61.  
  62.  
  63. struct      _timeb timebuffer;
  64. char *      timeline;
  65.  
  66.  
  67. struct            trap_criteria   {
  68. DWORD          tc_func_num;    //function app wants trapped (-1 = all)
  69. DWORD          tc_drive;       //drive (-1 = all drives)
  70. DWORD          tc_proc_handle; //process (-1 if all processes)
  71. DWORD          tc_vm_num;      //VMs (-1 if all) (0x0fffffffe if only DOS)
  72. WORD           tc_status;      //error code (-1 if all status)
  73. DWORD          tc_callback;    //address to call back
  74. WORD           tc_misc;        //misc flags for OpenTrap
  75.                                 };
  76.  
  77. struct  trap_record     {
  78. DWORD           tr_function;   
  79. DWORD           tr_drive;      
  80. unsigned char   tr_ir_flags;   
  81. WORD            tr_ir_options; 
  82. DWORD           tr_res_type;   
  83. DWORD           tr_handle;
  84. char            tr_file1[MAX_PATH];
  85. char            tr_file2[MAX_PATH];
  86. char            tr_program[9];
  87. DWORD           tr_vm;         
  88. DWORD           tr_proc_handle;
  89. WORD            tr_error;      
  90. DWORD           tr_drv_context;
  91. WORD            tr_drv_status;
  92. WORD            tr_drv_miscflag;
  93.                            };
  94.  
  95. struct          upcall  {
  96. DWORD           trap_rec_num;
  97. DWORD           trap_dat_ptr;
  98. WORD            trap_dat_len;
  99.                         };
  100.  
  101.  
  102.  
  103. struct  trap_criteria   trap_cond={IFSFN_OPEN ,(DWORD) LOG_ALL,(DWORD) LOG_ALL,(DWORD) LOG_ALL ,(WORD) LOG_ALL,NULL,(WORD) 0};
  104. //struct  trap_criteria   trap_cond2={IFSFN_READ ,(DWORD) LOG_ALL,(DWORD) LOG_ALL,(DWORD) LOG_ALL_DOS ,(WORD) LOG_ALL,NULL,(WORD) 0};
  105. struct  trap_criteria   trap_cond2={IFSFN_CLOSE ,(DWORD) LOG_ALL,(DWORD) LOG_ALL,(DWORD) LOG_ALL ,(WORD) LOG_ALL,NULL,(WORD) 0};
  106.  
  107. struct  upcall  trap_update;
  108.  
  109. unsigned char general_io_buf[512];      //holds data to/from Vxd
  110. char        data_buffer[128];           //holds data from edit controls
  111. DWORD Unobsfucator = 0;
  112. DWORD   condition_handle;               //how vxd references our callback
  113. DWORD   condition_handle2;              //how vxd references our callback
  114. char * rep_buf_ptr;
  115. char * start_rep_buf;
  116.  
  117.  
  118.  
  119.  
  120.  
  121. int WINAPI WinMain(HINSTANCE winhandle, HINSTANCE prevwinhandle,
  122.                         LPSTR cmdparam, int cmdshow)
  123. {
  124.        static char appname[]="opentrap";
  125.        hinstance=winhandle;
  126.  
  127.        MSG     message;
  128.        WNDCLASSEX windowclass;
  129.  
  130.        windowclass.style =     CS_HREDRAW | CS_VREDRAW;
  131.         windowclass.lpfnWndProc = regvproc;
  132.        windowclass.cbClsExtra = 0;
  133.        windowclass.cbWndExtra = 0;
  134.        windowclass.cbSize =    sizeof(WNDCLASSEX);
  135.        windowclass.hInstance = winhandle;   
  136.        windowclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  137.        windowclass.hIconSm= LoadIcon(NULL, IDI_APPLICATION);
  138.        windowclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  139.        windowclass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
  140.        windowclass.lpszMenuName=NULL;
  141.        windowclass.lpszClassName=appname;
  142.    
  143.  
  144.        RegisterClassEx(&windowclass);
  145.  
  146.        handle = CreateWindow (appname, "Load OpenTrap",
  147.                    WS_OVERLAPPEDWINDOW,
  148.                    CW_USEDEFAULT,
  149.                    CW_USEDEFAULT,
  150.                    CW_USEDEFAULT,
  151.                    CW_USEDEFAULT,
  152.                    NULL, NULL, winhandle, NULL);
  153.  
  154.        ShowWindow(handle, cmdshow);
  155.        UpdateWindow(handle);
  156.  
  157.        while(GetMessage(&message, NULL, 0,0))
  158.        {
  159.         TranslateMessage(&message);     //get key events
  160.         DispatchMessage(&message);
  161.     }        
  162.            return message.wParam;        
  163.  
  164. }
  165.  
  166.  
  167.  
  168. LRESULT CALLBACK regvproc(HWND hwnd, UINT message, 
  169.                                 WPARAM wParam, LPARAM lParam)
  170. {
  171.     HDC         hdc;
  172.     PAINTSTRUCT    pntstruct;
  173.     SIZE        extent;
  174.             
  175.     static HMENU hmenu;
  176.     static TEXTMETRIC tm;
  177.     static int  client_x_size;
  178.     static int  client_y_size;
  179.     static int  xpos;
  180.     static int  ypos;
  181.     static int  yinc;
  182.     static int  linespacing;
  183.     unsigned int max_lines;
  184.     unsigned int i;
  185.  
  186.     switch(message)
  187.     {
  188.  
  189.  
  190.     case WM_CREATE:
  191.  
  192.  
  193.         rep_buf_ptr=(char *) malloc (REP_BUF_SIZE);
  194.  
  195.         if ( rep_buf_ptr == NULL )
  196.         {
  197.  
  198.             MessageBox(hwnd, "Can't malloc", "!!!!!!!!!!!!!!!",
  199.                   MB_ICONINFORMATION);
  200.             return -1;
  201.         }
  202.  
  203.  
  204.         *rep_buf_ptr='R';
  205.         *(rep_buf_ptr + 1)='I';
  206.  
  207.  
  208.         
  209.         start_rep_buf=rep_buf_ptr;
  210.  
  211.         InitUnobsfucator();
  212.  
  213.         // open the handle to the VXD
  214.         vxd_handle = CreateFile( VXD_NAME, 0, 0, NULL,
  215.                                     0, FILE_FLAG_DELETE_ON_CLOSE, NULL );
  216.  
  217.         if ( vxd_handle == INVALID_HANDLE_VALUE )
  218.         {
  219.  
  220.             MessageBox(hwnd, "Can't access opentrap.vxd", "Vxd NOT Present",
  221.                   MB_ICONINFORMATION);
  222.             return -1;
  223.         }
  224.  
  225.         DeviceIoControl(    vxd_handle, VXD_GET_VER,
  226.                                     start_rep_buf, 4, NULL, 0, NULL, NULL );
  227.  
  228.         trap_cond.tc_callback=(DWORD) &our_callback;
  229.         trap_cond2.tc_callback=(DWORD) &our_callback;
  230.         
  231.         InitializeCriticalSection(&crit_sec);
  232.         
  233.         stop_flag = CreateEvent(NULL, FALSE, FALSE, NULL); 
  234.  
  235.         ResetEvent(stop_flag);    
  236.  
  237.  
  238.         thread_handle=(HANDLE) _beginthreadex(NULL, 4096,(unsigned int (__stdcall *) (void *)) do_getcalls,
  239.                     NULL, 0,(unsigned int *) &thread_id);
  240.  
  241.  
  242.        MessageBox(hwnd, "Loaded opentrap.vxd", "Everythings Cool",
  243.                     MB_ICONINFORMATION);
  244.  
  245.        
  246.         return 0;
  247.  
  248.  
  249.     case WM_DESTROY:
  250.         
  251.        HANDLE file_handle;
  252.        DWORD havewritten;
  253.  
  254.         trap_update.trap_rec_num=condition_handle;
  255.         trap_update.trap_dat_ptr=(DWORD) &trap_cond;
  256.         trap_update.trap_dat_len=sizeof(struct trap_criteria);
  257.  
  258.         trap_cond.tc_misc=(APP_EXITING | DISABLE_LOGGING);
  259.  
  260.         DeviceIoControl(vxd_handle,VXD_UPDATE, &trap_update,
  261.                    (sizeof(struct upcall)),NULL,
  262.                    0, NULL, NULL );
  263.  
  264.         trap_update.trap_rec_num=condition_handle2;
  265.         trap_update.trap_dat_ptr=(DWORD) &trap_cond2;
  266.         trap_update.trap_dat_len=sizeof(struct trap_criteria);
  267.  
  268.         trap_cond2.tc_misc=(APP_EXITING | DISABLE_LOGGING);
  269.  
  270.           DeviceIoControl(vxd_handle,VXD_UPDATE, &trap_update,
  271.                    (sizeof(struct upcall)),NULL,
  272.                    0, NULL, NULL );     
  273.  
  274.        CloseHandle(vxd_handle);
  275.  
  276.  
  277.        SetEvent(stop_flag);            //tell logging thread to stop
  278.  
  279.        CloseHandle(thread_handle);
  280.        
  281.        file_handle=CreateFile("logdat.txt", GENERIC_WRITE,
  282.                        0, NULL, CREATE_ALWAYS, 0,0);
  283.  
  284.        WriteFile(file_handle, start_rep_buf, (rep_buf_ptr - start_rep_buf),
  285.                        &havewritten, NULL);
  286.  
  287.        CloseHandle(file_handle);
  288.  
  289.         DeleteCriticalSection(&crit_sec);
  290.  
  291.       
  292.        free(start_rep_buf);
  293.  
  294.        PostQuitMessage(0);
  295.        return 0L;
  296.  
  297.  
  298.     default:
  299.  
  300.         return DefWindowProc(hwnd, message, wParam, lParam);
  301.  
  302.     } //end switch of messages passed to callback
  303.  
  304. }
  305.  
  306.  
  307. DWORD  __stdcall do_getcalls()
  308. {
  309.  
  310.        //Set up the callback with the VxD (note: VxD will get the
  311.        //id for this thread and use it and the callback address to
  312.        //inform us about file opens)
  313. DeviceIoControl(        vxd_handle, VXD_REGISTER_CB, &trap_cond,
  314.                    (sizeof(struct trap_criteria)),&condition_handle,
  315.                    sizeof(condition_handle), NULL, NULL );
  316.  
  317.  
  318.   DeviceIoControl(        vxd_handle, VXD_REGISTER_CB, &trap_cond2,
  319.                    (sizeof(struct trap_criteria)),&condition_handle2,
  320.                    sizeof(condition_handle2), NULL, NULL ); 
  321.  
  322.  
  323.  
  324.  
  325.        while( WaitForSingleObjectEx(stop_flag, INFINITE, TRUE) == WAIT_IO_COMPLETION);
  326.  
  327.        return 0;
  328. }
  329.  
  330.  
  331. void __stdcall our_callback(DWORD trap_rec_ptr)
  332. {
  333.  
  334.     format_log_record( (struct trap_record *) trap_rec_ptr);
  335.    release_rec(trap_rec_ptr);
  336.  
  337.  
  338. }
  339.  
  340. void format_log_record (struct trap_record * tr_ptr)
  341. {
  342. char * work_ptr;
  343.  
  344.  
  345. if ( (rep_buf_ptr + 240) > (start_rep_buf + REP_BUF_SIZE)) return;
  346.  
  347.  
  348.     EnterCriticalSection(&crit_sec);
  349.  
  350.    _ftime(&timebuffer);
  351.    timeline=ctime( & (timebuffer.time));
  352.  
  353.    rep_buf_ptr+=(sprintf(rep_buf_ptr, "File: %s \n", tr_ptr->tr_file1));
  354.    rep_buf_ptr+=(sprintf(rep_buf_ptr, "ir_flags: %02x  io_options: %04x  ", (unsigned) tr_ptr->tr_ir_flags, 
  355.                     tr_ptr->tr_ir_options));
  356.  
  357.    rep_buf_ptr+=(sprintf(rep_buf_ptr, "reenter: %08x  VM: %04x  func: %04x\n", (unsigned) tr_ptr->tr_res_type, 
  358.                     tr_ptr->tr_vm, tr_ptr->tr_function));
  359.    
  360.    rep_buf_ptr+=(sprintf(rep_buf_ptr, "drive: %04x  status: %04x  File accessed by: %s \n ", tr_ptr->tr_drive, 
  361.                     tr_ptr->tr_error, tr_ptr->tr_program));
  362.  
  363.     if (tr_ptr->tr_function == IFSFN_OPEN ) work_ptr="OPEN";
  364.     else
  365.     if (tr_ptr->tr_function == IFSFN_CLOSE ) work_ptr="CLOSE";
  366.  
  367.    rep_buf_ptr+=(sprintf(rep_buf_ptr, "function: %s  Handle: %04x  Time: %.8s.%hu  \n \n", 
  368.                     work_ptr, tr_ptr->tr_handle, &timeline[11], timebuffer.millitm));
  369.  
  370.     LeaveCriticalSection(&crit_sec);
  371.  
  372. }
  373.  
  374.  
  375. void    release_rec(DWORD rec_address)
  376. {
  377.  
  378.  
  379.         DeviceIoControl(    vxd_handle, VXD_REC_DONE,
  380.                                                         (void *) &rec_address, 4, NULL, 0, NULL, NULL );
  381.  
  382. }
  383.  
  384.  
  385. void InitUnobsfucator(void)
  386. {
  387.     DWORD   tid;
  388.     
  389.     tid = GetCurrentThreadId();
  390.     
  391.     __asm {
  392.             push    es
  393.             mov     ax, fs
  394.             mov     es, ax
  395.             mov     eax, 18h
  396.             mov     eax, es:[eax]
  397.             sub     eax, 10h
  398.             xor     eax,[tid]
  399.             mov     [Unobsfucator], eax
  400.             pop        es
  401.     }
  402. }
  403.